<?php

namespace app\seller\controller;
use app\common\controller\Seller;

use app\common\model\City;
use app\common\model\FareTmpl as FareTmplModel;
use app\common\model\CarryMode;
use app\common\model\InclPostageProviso;

class FareTmpl extends Seller
{
    
    public function index(){
        
        $userId = $this->getSellerId();

        $list = FareTmplModel::all(['user_id'=>$userId]);
        
        foreach ($list as $fare){
            $fare['default'] = CarryMode::where(['FareId'=>$fare['id'],'IsDefault'=>1])->find();
            $fare['carryMode'] = CarryMode::where(['FareId'=>$fare['id'],'IsDefault'=>0])->select();
            $fare['proviso'] = InclPostageProviso::get(['FareId'=>$fare['id']]);
        }
        $this->assign('list',$list);
        return $this->fetch();
    }
    
    public function add()
    {
        $this->assign('user_id',$this->getSellerId());
        $this->assign('provienceList',City::all(['parent_id'=>22]));
        return parent::add(); // TODO: Change the autogenerated stub
    }
    
    public function save(){
        
        $params = paramForm();
        
        $carryModelProviences = $params['provience'];//省份列表，如果是默认费用则该值为0
        $carryModelFirstAmounts = $params['FirstAmount'];//首费
        $carryModelSecondAmount = $params['SecondAmount'];//续费
        $carryModelFirstPieces = $params['FirstPiece'];//首件、默认为1
        $carryModelSecondPieces = $params['SecondPiece'];//续件单位、默认为1
        $carryModelIsDefaults = $params['IsDefault'];//是否默认
        
        $isFare = FareTmplModel::get(['Name'=>$params['Name']]);
        if($isFare){
            $this->error('名称重复');
        }
        
        $fare = new FareTmplModel();
        $rtFareAdd = $fare->allowField(true)->save($params);
        
        if(!$rtFareAdd){
            $fare->delete();
            $this->error('新增失败');
            
        }
        
       
        $carryModeArr = array();
        foreach ($carryModelProviences as $idx=>$provience){
            
            //如果首费和续费没有填写的，非默认的配送方式，就过滤掉
            if(!$this->isValidate($carryModelFirstAmounts[$idx])||!$this->isValidate($carryModelSecondAmount[$idx])){
                continue;
            }
            
            $tempArr =[
                'Region'=>$provience,
                'FareId'=>$fare['id'],
                'FirstPiece'=>$carryModelFirstPieces[$idx],
                'FirstAmount'=>$carryModelFirstAmounts[$idx],
                'SecondPiece'=>$carryModelSecondPieces[$idx],
                'SecondAmount'=>$carryModelSecondAmount[$idx],
                'IsDefault'=>$carryModelIsDefaults[$idx]
            ];
    
            $carryModeArr[] = $tempArr;

        }
    
       
        $carryMode = new CarryMode();
        $rtCarryModel = $carryMode->saveAll($carryModeArr);
        
        if(!$rtCarryModel){
            $fare->delete();
            $this->error('新增快递信息失败');
        }
    
      
    
        $proviso =new InclPostageProviso();
        $proviso['Amount'] = $params['Amount'];
        $proviso['FareId'] = $fare['id'];
        $rtProviso = $proviso->save();
        
        if(!$rtProviso){
            $fare->delete();
            foreach ($rtCarryModel as $carryModel){
                $item = CarryMode::get($carryMode);
                $item->delete();
            }
            $this->error('新增包邮条件失败');
        }
        
        $this->success('添加邮费模板成功');
        
    }
    
    protected function isValidate($value){
        if($value==''){
            return false;
        }
        if(empty($value)){
            return false;
        }
        return true;
    }

}
